﻿using System;
using System.Data;
using System.Data.SqlServerCe;
using System.Collections.Generic;
using Home.Mobilesales.Common;
using Home.MobileSales.Common.Entities;


namespace Home.MobileSales.Common.Models
{
    public class OrderRecordRepository
    {
        public List<OrderRecord> GetOrderRecordByOrder(int orderId)
        {
            List<OrderRecord> records = new List<OrderRecord>();
            using (SqlCeConnection connection = WorkDataBase.CreateConnection())
            {
                if (connection.State != ConnectionState.Open)
                    connection.Open();
                string sqlQuery =
                    "SELECT IdSaleGood, Quantity, Price, GoodId " +
                    "FROM SalesGoods " +
                    "Where OrderId = @OrderId";
                using (SqlCeCommand com = new SqlCeCommand(sqlQuery, connection))
                {
                    com.Parameters.Add("@OrderId", orderId);

                    using (SqlCeDataReader dr = com.ExecuteReader())
                    {
                        int idSales = dr.GetOrdinal("IdSaleGood");
                        int quantity = dr.GetOrdinal("Quantity");
                        int price = dr.GetOrdinal("Price");
                        int goodId = dr.GetOrdinal("GoodId");
                        while (dr.Read())
                        {
                            OrderRecord orderRecord = new OrderRecord();
                            orderRecord.IdOrderRecord = dr.GetInt32(idSales);
                            orderRecord.Price = dr.GetDouble(price);
                            orderRecord.Quantity = dr.GetDouble(quantity);
                            orderRecord.Amount = Math.Round(orderRecord.Quantity * orderRecord.Price,2);
                            orderRecord.Good = DataManager.GetInstance().Goods.GetGoodById(dr.GetInt32(goodId));
                            records.Add(orderRecord);
                        }
                        return records;
                    }
                }
            }
        }

        public void DeleteOrderRecord(int orderRecordId)
        {
            using (SqlCeConnection connection = WorkDataBase.CreateConnection())
            {
                if (connection.State != ConnectionState.Open)
                    connection.Open();
                string sqlQuery =
                    "delete from SalesGoods where IdSaleGood = @IdSalesGood";
                using (SqlCeCommand com = new SqlCeCommand(sqlQuery, connection))
                {
                    com.Parameters.Add("@IdSalesGood", orderRecordId);
                    if (com.ExecuteNonQuery() < 1)
                    {
                        throw new OtherException("Не видалено жодного запису");
                    }
                }
            }

        }

        public void InsertOrderRecord(OrderRecord orderRecord, int orderId)
        {
            using (SqlCeConnection connection = WorkDataBase.CreateConnection())
            {
                if (connection.State != ConnectionState.Open)
                    connection.Open();
                string sqlQuery =
                    "insert into SalesGoods (OrderId, GoodId, Price, Quantity) values (@OrderId, @GoodId, @Price, @Quantity)";
                using (SqlCeCommand com = new SqlCeCommand(sqlQuery, connection))
                {
                    com.Parameters.Add("@OrderId", orderId);
                    com.Parameters.Add("@GoodId", orderRecord.Good.idGood);
                    com.Parameters.Add("@Price", orderRecord.Price);
                    com.Parameters.Add("@Quantity", orderRecord.Quantity);
                    if (com.ExecuteNonQuery() < 1)
                    {
                        throw new OtherException("Не добавлено жодного запису");
                    }
                }

            }
        }
    }
}
